#include <iostream>
using namespace std;
#include <vector>
#include <set>
//https://leetcode.cn/problems/longest-consecutive-sequence/?envType=study-plan-v2&envId=top-100-liked

int longestConsecutive(vector<int>& nums) {
    if (nums.empty()) {
        return 0;
    }
    set<int> s1;
    s1.insert(nums.begin(), nums.end());
    auto a = s1.begin();
    auto b = ++s1.begin();
    int len = 0;
    set<int> s2;
    while (a != s1.end() && b != s1.end()) {
        if (*a + 1 == *b) {
            len++;
        }
        else {
            s2.insert(len + 1);
            len = 0;
        }
        ++a;
        ++b;
    }
    s2.insert(len + 1);
    return *s2.rbegin();
}

int main(){
    vector<int> v = { 2,3,5,7,8,4,3,5,9 };
    cout << longestConsecutive(v);
	return 0; 
}